package com.yuqing.sentimentapp.home.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.media.ThumbnailUtils;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.linkface.baselibrary.utils.DateUtil;
import com.linkface.baselibrary.utils.DensityUtil;
import com.linkface.common.adaper.CommonRecycleAdapter;
import com.linkface.common.adaper.CommonViewHolder;
import com.linkface.common.adaper.MultiTypeSupport;
import com.yuqing.sentimentapp.R;
import com.yuqing.sentimentapp.home.bean.RecommendDataBean;
import com.yuqing.sentimentapp.login.bean.LoginResultBean;
import com.yuqing.sentimentapp.utils.DisplayUtil;
import com.yuqing.sentimentapp.utils.GlideUtil;
import com.yuqing.sentimentapp.utils.SharePrefrenceHelper;
import com.yuqing.sentimentapp.widget.jzvideo.Jzvd;
import com.yuqing.sentimentapp.widget.jzvideo.JzvdStd;

import java.util.List;

import wseemann.media.FFmpegMediaMetadataRetriever;

/**
 * Created by shenhe on 2018/9/19.
 */

public class RecommendAdapter extends CommonRecycleAdapter<RecommendDataBean.DataBean> implements MultiTypeSupport<RecommendDataBean.DataBean> {
    private CommonViewHolder.onItemCommonClickListener commonClickListener;
    private Context mContext;
    private RecommendBottomBtnClickListener bottomBtnClickListener;

    public RecommendBottomBtnClickListener getBottomBtnClickListener() {
        return bottomBtnClickListener;
    }

    public void setBottomBtnClickListener(RecommendBottomBtnClickListener bottomBtnClickListener) {
        this.bottomBtnClickListener = bottomBtnClickListener;
    }

    public RecommendAdapter(Context context, List<RecommendDataBean.DataBean> dataList) {
        super(context, dataList, R.layout.fragment_recommend_item_top);
        this.mContext = context;
    }

    public RecommendAdapter(Context context, List<RecommendDataBean.DataBean> dataList, CommonViewHolder.onItemCommonClickListener commonClickListener) {
        super(context, dataList, R.layout.fragment_recommend_item_top);
        this.mContext = context;
        this.commonClickListener = commonClickListener;
        this.multiTypeSupport = this;
    }

    @Override
    public void bindData(final CommonViewHolder holder, final RecommendDataBean.DataBean data) {
        String isRead = data.getIsRead();
        if (!TextUtils.isEmpty(isRead) && TextUtils.equals("1", isRead)) {
            ((TextView) holder.getView(R.id.tv_title)).setTextColor(mContext.getResources().getColor(R.color.color_aaaaaa));
        } else {
            ((TextView) holder.getView(R.id.tv_title)).setTextColor(mContext.getResources().getColor(R.color.color_333333));
        }
        if (TextUtils.equals("1", data.getIsTop())) {//置顶
            holder.setText(R.id.tv_title, data.getTitle())
                    .setText(R.id.tv_date, data.getCreateAt());
        } else if (TextUtils.equals("N", data.getType())){//新闻
            holder.setText(R.id.tv_title, data.getTitle())
                    .setText(R.id.tv_date, data.getCreateAt())
                    .setText(R.id.tv_special_topic, "新闻")
                    .setText(R.id.tv_related, "相关 " + 0);
        } else if (TextUtils.equals("Z", data.getType())) {//即时专题
            holder.setText(R.id.tv_title, data.getTitle())
                    .setText(R.id.tv_date, data.getCreateAt())
                    .setText(R.id.tv_special_topic, "即时专题")
                    .setText(R.id.tv_related, "相关 " + 0);
        } else if (TextUtils.equals("F", data.getType())) {//专题分析
            holder.setText(R.id.tv_title, data.getTitle())
                    .setText(R.id.tv_date, data.getCreateAt())
                    .setText(R.id.tv_special_topic, "专题分析")
                    .setText(R.id.tv_related, "相关 " + 0);
        } else {
            if (TextUtils.isEmpty(data.getSentiment())) {
                holder.getView(R.id.tv_top).setVisibility(View.GONE);
            } else {
                switch (data.getSentiment()) {
                    case "A":
                        holder.setText(R.id.tv_top, "正面");
                        break;
                    case "B":
                        holder.setText(R.id.tv_top, "负面");
                        break;
                    case "C":
                        holder.setText(R.id.tv_top, "敏感");
                        break;
                }
                holder.getView(R.id.tv_top).setVisibility(View.VISIBLE);
            }
            String caption = data.getCaption();
            if (TextUtils.isEmpty(caption)) caption = "";
            holder.setText(R.id.tv_title, caption + data.getTitle())
                    .setText(R.id.tv_source, TextUtils.isEmpty(data.getSource()) ? "" : data.getSource())
                    .setText(R.id.tv_time, DateUtil.formatDateTime(data.getDatatime()))
                    .setText(R.id.tv_devp_issued, data.getSubStatus());
            String replyId = data.getReplyId();
            String pubId = data.getPubId();
            //是否是视频
            if (data.isVideo()) {
                JzvdStd jzvdStd = holder.getView(R.id.videoplayer);
                jzvdStd.setUp(data.getVideoUrl(), data.getTitle(), Jzvd.SCREEN_NORMAL);
                if (TextUtils.isEmpty(data.getVideoThumb())) {
                    FFmpegMediaMetadataRetriever fmmr = new FFmpegMediaMetadataRetriever();
                    try {
                        fmmr.setDataSource(data.getVideoUrl());
                        Bitmap bitmap = fmmr.getFrameAtTime();
                        if (bitmap != null) {
                            Bitmap b2 = fmmr
                                    .getFrameAtTime(
                                            4000000,
                                            FFmpegMediaMetadataRetriever.OPTION_CLOSEST_SYNC);
                            if (b2 != null) {
                                bitmap = b2;
                            }
                            if (bitmap.getWidth() > 640) {// 如果图片宽度规格超过640px,则进行压缩
                                bitmap = ThumbnailUtils.extractThumbnail(bitmap,
                                        DisplayUtil.getScreenWidth(mContext) - DensityUtil.dip2px(mContext, 40), DensityUtil.dip2px(mContext, 200),
                                        ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
                            }
                            jzvdStd.thumbImageView.setImageBitmap(bitmap);
                        }
                    } catch (IllegalArgumentException ex) {
                        ex.printStackTrace();
                        jzvdStd.thumbImageView.setImageResource(R.drawable.ic_default_big);
                    } finally {
                        if (fmmr != null) {
                            fmmr.release();
                        }
                    }
                } else {
                    GlideUtil.getImageWithErrorLoadingImg(mContext, data.getVideoThumb(), jzvdStd.thumbImageView, R.drawable.ic_default_big, R.drawable.ic_default_big);
                }

            } else {//图文
                String imageUrl = data.getImageUrl();

                if (!TextUtils.isEmpty(imageUrl)) {
                    String[] images = imageUrl.split(";");
                    if (images.length < 3){//一张图
                        GlideUtil.loadRoundImage(mContext, images[0], holder.getView(R.id.iv_pic), 3);
                    } else {//三张图
                        GlideUtil.loadRoundImage(mContext, images[0], holder.getView(R.id.iv_pic_one), 3);
                        GlideUtil.loadRoundImage(mContext, images[1], holder.getView(R.id.iv_pic_two), 3);
                        GlideUtil.loadRoundImage(mContext, images[2], holder.getView(R.id.iv_pic_three), 3);
                    }
                }
            }

            LoginResultBean.DataBean.UserBean user = SharePrefrenceHelper.getUser();
            String deptLevel = "";
            if (user != null && user.getDeptVo() != null) {
                deptLevel = user.getDeptVo().getDeptLevel();
            }
            String finalDeptLevel = deptLevel;

            //回复
            updateReplyView(holder, finalDeptLevel);

            //上报
            updateReportView(holder, replyId, finalDeptLevel);

            //下发
            updateIssuedView(holder, pubId, finalDeptLevel);

            //隐藏
            holder.getView(R.id.ll_hide).setOnClickListener(v -> {
                if (bottomBtnClickListener != null) {
                    bottomBtnClickListener.hideClickListener(holder.getAdapterPosition() - 1);
                }
            });
        }
        holder.setCommonClickListener(commonClickListener);


    }

    private void updateIssuedView(CommonViewHolder holder, String pubId, String finalDeptLevel) {
        // 支队不可以下发
        // 已下发不可以再次下发
        if (!TextUtils.isEmpty(pubId) || (!TextUtils.isEmpty(finalDeptLevel) && TextUtils.equals(finalDeptLevel, "2"))) {
            ((TextView) holder.getView(R.id.tv_issued)).setTextColor(mContext.getResources().getColor(R.color.color_aaaaaa));
        } else {
            ((TextView) holder.getView(R.id.tv_issued)).setTextColor(mContext.getResources().getColor(R.color.color_0d7dff));
        }
        holder.getView(R.id.ll_issued).setOnClickListener(v -> {
            // 支队不可以下发
            if (!TextUtils.isEmpty(finalDeptLevel) && TextUtils.equals(finalDeptLevel, "2")) {
                return;
            }
            if (!TextUtils.isEmpty(pubId)) {// 已下发不可以再次下发
                return;
            }
            if (bottomBtnClickListener != null) {
                bottomBtnClickListener.issuedClickListener(holder.getAdapterPosition() - 1);
            }
        });
    }

    private void updateReportView(CommonViewHolder holder, String replyId, String finalDeptLevel) {
        if (TextUtils.isEmpty(replyId)) {// 未上报过
            holder.setText(R.id.tv_report, "上报");
        } else {// 已上报，显示重报
            holder.setText(R.id.tv_report, "重报");
        }
        // 部局身份 上报不可点击
        if (!TextUtils.isEmpty(finalDeptLevel) && TextUtils.equals("0", finalDeptLevel)) {
            ((TextView) holder.getView(R.id.tv_report)).setTextColor(mContext.getResources().getColor(R.color.color_aaaaaa));
        } else {
            ((TextView) holder.getView(R.id.tv_report)).setTextColor(mContext.getResources().getColor(R.color.color_0d7dff));
        }

        holder.getView(R.id.ll_report).setOnClickListener(v -> {
            // 部局身份 上报不可点击
            if (!TextUtils.isEmpty(finalDeptLevel) && TextUtils.equals("0", finalDeptLevel)) {//部门级别：0部级1省级2市级
                return;
            }
            if (bottomBtnClickListener != null) {
                bottomBtnClickListener.reportClickListener(holder.getAdapterPosition() - 1);
            }
        });
    }

    private void updateReplyView(CommonViewHolder holder, String finalDeptLevel) {
        // 支队不可以回复
        if (!TextUtils.isEmpty(finalDeptLevel) && TextUtils.equals(finalDeptLevel, "2")) {
            ((TextView) holder.getView(R.id.tv_reply)).setTextColor(mContext.getResources().getColor(R.color.color_aaaaaa));
        } else {
            ((TextView) holder.getView(R.id.tv_reply)).setTextColor(mContext.getResources().getColor(R.color.color_0d7dff));
        }
        holder.getView(R.id.ll_reply).setOnClickListener(v -> {
            // 支队不可以回复
            if (!TextUtils.isEmpty(finalDeptLevel) && TextUtils.equals(finalDeptLevel, "2")) {
                return;
            }
            if (bottomBtnClickListener != null) {
                bottomBtnClickListener.replyClickListener(holder.getAdapterPosition() - 1);
            }
        });
    }

    @Override
    public int getLayoutId(RecommendDataBean.DataBean item, int position) {
        if (item.isVideo()) return R.layout.item_videoview_recommend;
        if (TextUtils.equals("1", item.getIsTop())) {
            return R.layout.fragment_recommend_item_top;
        }
        if (TextUtils.equals("N", item.getType()) ||
                TextUtils.equals("Z", item.getType()) ||
                TextUtils.equals("F", item.getType())) {
            return R.layout.fragment_recommend_item_topic;
        }
        String imageUrl = item.getImageUrl();
        if (TextUtils.isEmpty(imageUrl)) {
            return R.layout.fragment_recommend_item_no_pic;
        }
        String[] images = imageUrl.split(";");
        if (images.length == 0) {
            return R.layout.fragment_recommend_item_no_pic;
        } else if (images.length < 3){
            return R.layout.fragment_recommend_item_one_pic;
        } else {
            return R.layout.fragment_recommend_item_three_pic;
        }
    }

    public interface RecommendBottomBtnClickListener {
        void replyClickListener(int position);
        void reportClickListener(int position);
        void issuedClickListener(int position);
        void hideClickListener(int position);
    }
}
